//index.js
var Util = require('../../utils/util.js')
//获取应用实例
var app = getApp()
Page({
  data: {
     cityList:[],
     cityNameList:[],
     hospitalList:[],
     cityIndex:0,
     pageNum:1,
     urlprefix:"http://tnfs.tngou.net/img"
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    this.getCityList()
  },
  //得到城市列表
  getCityList:function(e){
      var that = this
      wx.showToast({title:"加载中...",icon:"loading",duration:10000})
      wx.request({
        url: 'https://apis.baidu.com/tngou/hospital/city',
        data: {
          "type":'all'
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          "apikey":"da9ab87de033d3b01b34e35d5bfac404"
        }, // 设置请求的 header
        success: function(res){
          that.setData({"json":res.data})
          var cityList = []
          var cityNameList = []
          if(res.data.status == true){
            console.log("获取成功")
            var cityArray = res.data.tngou;
            for(var i = 0; i < cityArray.length;i++){
              var city = {}
              var temp = cityArray[i];
              city.id = temp.id;
              city.province = temp.province;
              city.level = temp.level;
              cityList.push(city)
              cityNameList.push(city.province)
            }
            that.setData({"cityList":cityList,"cityNameList":cityNameList})
            that.getHospitalList(that.data.pageNum)
          }else{
            console.log('失败')
          }
        },
        fail: function() {
          // fail
          console.log("获取失败")
        },
        complete: function() {
          // complete
        }
      })
  },
  //选择城市
  selectCity:function(e){
      var num = e.detail.value
      this.setData({"cityIndex":num})
      this.data.pageNum = 1
      this.getHospitalList(this.data.pageNum)
  },
  //得到根据城市获取的医院列表
  getHospitalList:function(count){
    wx.showToast({title:"加载中...",icon:"loading",duration:10000})
      var that = this
      var city = {}
      city = this.data.cityList[this.data.cityIndex]
      console.log("----"+city)
      var cityid = city.id
      wx.request({
        url: 'https://apis.baidu.com/tngou/hospital/list',
        data: {
          "id":cityid,
          "page":that.data.pageNum,
          "rows":10
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          "apikey":"da9ab87de033d3b01b34e35d5bfac404"
        }, // 设置请求的 header
        success: function(res){
          console.log(res.data)
          
          var cureenthList = that.data.hospitalList
          if(count == 1){
            cureenthList = []
          }
          for (var hospital in res.data.tngou){
              cureenthList.push(res.data.tngou[hospital])
          }
          that.setData({"hospitalList":cureenthList,"pageNum":count + 1})
        },
        fail: function() {
          // fail
        },
        complete: function() {
          // complete
          wx.hideToast()
        }
      })

  },
  //加载更多的方法
  loadMore:function(){
    console.log("加载更多")
    this.getHospitalList(this.data.pageNum)
  },
  //点击某一条列表 进入下一页
  selectDetail:function(e){
    var index = e.currentTarget.dataset.index
    var hospitalname = this.data.hospitalList[index].name
    wx.navigateTo({
      url: '../hospitalDetail/hospitalDetail?hospitalname=' + hospitalname,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  }
  
})
